/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class mail4 {

    private final String from;
    private final String to;
    private final String subject;
    private final String text;

    public mail4(String from, String to, String subject, String text){

        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    public void send(){

        Properties props = new Properties();
        props.put("mail.smtp.host", "nhom5.no-ip.org");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable", "true");


        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {

            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            Transport.send(simpleMessage);


        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {

        String from = "sinhvien1@nhom5.no-ip.org";
        String to = "hoangthach@nhom5.no-ip.org";
        String subject = "Đây là cái tiêu đề của mail4  nek";
        String message = "Đây là nội dung của mail há há";
        mail4 sendMail = new mail4(from, to, subject, message);
        sendMail.send();
         System.out.println("Mail da duoc gui..................."); // thông báo thành công
    }
}




//main class
//***********




